<template>
    <div class="">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-copy"></i> 公告栏</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-tabs v-model="message">
                <el-tab-pane :label="`未读消息(${unread.length})`" name="first">
                    <el-table :data="unread" :show-header="false" style="width: 100%">
                        <el-table-column>
                            <template slot-scope="scope">
                                <span class="message-title">{{scope.row.announcementContent}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column>
                            <template slot-scope="scope">
                                <span class="message-title">{{scope.row.announcementTime}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column width="120">
                            <template slot-scope="scope">
                                <el-button size="small" @click="handleRead(scope.$index)">标为已读</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="handle-row">
                        <el-button type="primary">全部标为已读</el-button>
                    </div>
                </el-tab-pane>
                <el-tab-pane :label="`已读消息(${read.length})`" name="second">
                    <template v-if="message === 'second'">
                        <el-table :data="read" :show-header="false" style="width: 100%">
                            <el-table-column>
                                <template slot-scope="scope">
                                    <span class="message-title">{{scope.row.announcementContent}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column>
                                <template slot-scope="scope">
                                    <span class="message-title">{{scope.row.announcementTime}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column width="120">
                                <template slot-scope="scope">
                                    <el-button type="danger" @click="handleDel(scope.$index)">删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class="handle-row">
                            <el-button type="danger">删除全部</el-button>
                        </div>
                    </template>
                </el-tab-pane>
                <el-tab-pane :label="`回收站(${recycle.length})`" name="third">
                    <template v-if="message === 'third'">
                        <el-table :data="recycle" :show-header="false" style="width: 100%">
                            <el-table-column>
                                <template slot-scope="scope">
                                    <span class="message-title">{{scope.row.announcementContent}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column>
                                <template slot-scope="scope">
                                    <span class="message-title">{{scope.row.announcementTime}}</span>
                                </template>
                            </el-table-column>
                            <el-table-column width="120">
                                <template slot-scope="scope">
                                    <el-button @click="handleRestore(scope.$index)">还原</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class="handle-row">
                            <el-button type="danger">清空回收站</el-button>
                        </div>
                    </template>
                </el-tab-pane>
            </el-tabs>
        </div>
    </div>
</template>

<script>
    export default {
        name: 'tabs',
        data() {
            return {
                message: 'first',
                showHeader: false,
                unread: [],
                read: [],
                recycle: []
            }
        },

        methods: {
            acceptData(){
                const _this = this
                _this.$axios.get("http://localhost:8888/announcement/list").then(res => {
                    console.log("okl")
                    console.log(res)
                    for (var i = 0;i<res.data.data.length;i++){
                        if(res.data.data[i].announcementState===0){
                            _this.unread.push(res.data.data[i])
                        }
                        if(res.data.data[i].announcementState===1){
                            _this.read.push(res.data.data[i])
                        }
                        if(res.data.data[i].announcementState===2){
                            _this.recycle.push(res.data.data[i])
                        }
                    }
            });
            },
            handleRead(index) {
                const item = this.unread.splice(index, 1);
                console.log(item[index])
                console.log(item[index].id)
                this.$axios.get("http://localhost:8888/announcement/updateState",{
                    params:{
                        id: item[index].id,
                        state: item[index].announcementState
                    }
                }).then(res => {
                    this.created
                });
                this.read = item.concat(this.read);

            },
            handleDel(index) {
                const item = this.read.splice(index, 1);
                console.log(item[index])
                console.log(item[index].id)
                this.$axios.get("http://localhost:8888/announcement/updateState",{
                    params:{
                        id: item[index].id,
                        state: item[index].announcementState
                    }
                }).then(res => {
                    this.created
                });
                this.recycle = item.concat(this.recycle);
            },
            handleRestore(index) {
                const item = this.recycle.splice(index, 1);
                console.log(item[index])
                console.log(item[index].id)
                this.$axios.get("http://localhost:8888/announcement/updateState",{
                    params:{
                        id: item[index].id,
                        state: item[index].announcementState
                    }
                }).then(res => {
                    this.created
                });
                this.read = item.concat(this.read);
            }
        },

        created() {
            console.log("7777777777888887777777")
            this.acceptData();

        },
        computed: {
            unreadNum() {
                return this.unread.length;
            }
        }
    }

</script>

<style>
    .message-title {
        cursor: pointer;
    }

    .handle-row {
        margin-top: 30px;
    }
</style>

<!--{-->
<!--announcementTime: '2018-04-19 20:00:00',-->
<!--announcementContent: '【系统通知】该系统将于今晚凌晨2点到5点进行升级维护',-->
<!--}, {-->
<!--announcementTime: '2018-04-19 21:00:00',-->
<!--announcementContent: '今晚12点整发大红包，先到先得',-->
<!--}-->
